package com.jeecms.wicool.dao.util.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.util.DictionaryDao;
import com.jeecms.wicool.entity.util.Dictionary2;

@Repository
public class DictionaryDaoImpl extends HibernateBaseDao<Dictionary2, Integer> implements DictionaryDao {
	public Pagination getPage(String typeCode,int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from Dictionary2 bean where 1=1");
		if (!StringUtils.isBlank(typeCode)) {
			f.append(" and bean.typeCode like :typeCode");
			f.setParam("typeCode", "%" + typeCode + "%");
		}
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}
	
	@SuppressWarnings("unchecked")
	public List<Dictionary2> getList(String typeCode){
		Criterion cron = Restrictions.like("typeCode",typeCode); 
		return findByCriteria(cron);
	}
	
	@SuppressWarnings("unchecked")
	public List<String> getTypeList(){
		Finder f=Finder.create("select  typeCode from Dictionary2 dic group by typeCode ");
		return find(f);
	}

	public Dictionary2 findById(Integer id) {
		Dictionary2 entity = get(id);
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	public Dictionary2 findByValue(String typeCode,String dicName){
		Criterion cron_type=null,cron_value=null;
		if(StringUtils.isNotBlank(typeCode)){
			cron_type = Restrictions.like("typeCode",typeCode);
		}
		if(StringUtils.isNotBlank(dicName)){
			cron_value = Restrictions.like("dicName",dicName);
		}
		List<Dictionary2>li=findByCriteria(cron_type,cron_value);
		if(li!=null&&li.size()>0){
			return li.get(0);
		}else{
			return null;
		}
	}
	@SuppressWarnings("unchecked")
	public Dictionary2 findByDicCode(String typeCode,Short dicCode){
		Finder f = Finder.create("select bean from Dictionary2 bean where 1=1 ");
		if(!StringUtils.isBlank(typeCode)){
			f.append(" and bean.typeCode = :typeCode");
			f.setParam("typeCode", typeCode);
		}
		if(dicCode!=null){
			f.append(" and bean.dicCode = :dicCode");
			f.setParam("dicCode", dicCode);
		}
		f.append(" order by bean.id desc");
		List<Dictionary2>li = find(f);
		if(li!=null&&li.size()>0){
			return li.get(0);
		}else{
			return null;
		}
	}
	public Dictionary2 save(Dictionary2 bean) {
		getSession().save(bean);
		return bean;
	}

	public Dictionary2 deleteById(Integer id) {
		Dictionary2 entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	public int countByValue(String dicName, String typeCode) {
		String hql = "select count(*) from Dictionary2 bean where bean.dicName=:dicName and bean.typeCode=:typeCode";
		Query query = getSession().createQuery(hql);
		query.setParameter("dicName", dicName);
		query.setParameter("typeCode", typeCode);
		return ((Number) query.iterate().next()).intValue();
	}
	
	@Override
	protected Class<Dictionary2> getEntityClass() {
		return Dictionary2.class;
	}
}